//
//  VisitorView.swift
//  SHWeibo2.0
//
//  Created by 黄礼潮 on 16/2/22.
//  Copyright © 2016年 黄礼潮. All rights reserved.
//

import UIKit
import SnapKit

protocol visitorViewDelegate:NSObjectProtocol{
   //注册
   func visitorViewDidRegister()
   //登录
   func visitorViewDidLogin()


}

class VisitorView: UIView {
    
    //定义一个代理属性
    weak var delegate = visitorViewDelegate?()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
        
    }
    //注册
    @objc private func clickRegisterButton(){
    
        print("点击注册")
        delegate?.visitorViewDidRegister()
        
    }
    //登录
    @objc private func clickLoginButton(){
    
       print("点击登录")
        delegate?.visitorViewDidLogin()
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //图标图像
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    //添加遮罩视图
    private lazy var maskIconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子
    private lazy var homeView:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    //添加文字
    private lazy var messageLabel:UILabel = UILabel(cztitle: "关注一些人，回这里看看有什么惊喜", color: UIColor.darkGrayColor())
    //添加注册按钮
    private lazy var registerButton:UIButton = UIButton(cz_butitle: "注册", backImageName: "common_button_white_disable", color: UIColor.orangeColor(), fontSize: 17)
    //添加登录按钮
    private lazy var loginButton:UIButton = UIButton(cz_butitle: "登录", backImageName: "common_button_white_disable")
    
    
}

extension VisitorView {
    
    func setupInfo(imageName:String? = nil,title:String){
        
        guard let imageName = imageName else{
            //开始动画
            animationVisitor()
            
            return
        }
        messageLabel.text = title
        
        //设置文字的对齐方式
        messageLabel.textAlignment = .Center
        
        iconView.image = UIImage(named: imageName)
        
        homeView.hidden = true
        
        
    }
    
   private  func animationVisitor(){
    
        let ani = CABasicAnimation(keyPath:"transform.rotation")
        ani.toValue = M_PI * 2
        ani.duration = 20
        ani.repeatCount = MAXFLOAT
    // 提示：对于需要循环播放的动画，可以设置此属性为 false
    // 视图离开屏幕后，不会被销毁！当 动画所在视图被销毁后，动画会连带销毁
    // 不需要考虑释放问题
        ani.removedOnCompletion = false
    
        iconView.layer.addAnimation(ani, forKey:nil)
    
    
    }


    func setUpUI(){
        
        //添加子控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //自定布局
        
        
        iconView.snp_makeConstraints { (make) -> Void in
           make.centerX.equalTo(self)
           make.centerY.equalTo(self).offset(-60)
        }
        homeView.snp_makeConstraints { (make) -> Void in
           make.centerX.equalTo(iconView)
           make.centerY.equalTo(iconView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.top.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 224, height: 36))
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 100, height: 30))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(CGSize(width: 100, height: 30))
        }
        //添加遮罩视图自动布局
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(registerButton)
        }
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
     //添加监听方法
        registerButton.addTarget(self, action:"clickRegisterButton", forControlEvents: .TouchUpInside)
        loginButton.addTarget(self, action: "clickLoginButton", forControlEvents:.TouchUpInside)
}



}
